package test;

import java.util.Stack;

public class MyQueue {
    //力扣 232. 用栈实现队列
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    public MyQueue() {
        this.stack1 = new Stack<>();
        this.stack1 = new Stack<>();

    }

    public void push(int x) {
        //把元素全部放到第一个栈
        if (stack2.isEmpty()) {
            stack1.push(x);
        } else {
            //先把栈2的元素全部放到栈1再添加新元素
            while (stack2.size() != 0) {
                int tmp = stack2.pop();
                stack1.push(tmp);
            }
            stack1.push(x);
        }

    }

    public int pop() {
        if (stack1.isEmpty() && stack2.isEmpty()) {
            return -1;
        }

        if (stack1.isEmpty()) {
            return stack2.pop();
        } else {
            while (stack1.size() != 0) {
                Integer val = stack1.pop();
                stack2.push(val);
            }
            return stack2.pop();
        }
    }

    public int peek() {

        if (stack1.isEmpty() && stack2.isEmpty()) {
            return -1;
        }

        if (stack1.isEmpty()) {
            return stack2.peek();
        }
        else {
            while (stack1.size() != 0) {
                Integer val = stack1.pop();
                stack2.push(val);
            }
            return stack2.peek();
        }
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}
